local M  = {
	gRect = nil,
	bindedObj = nil
}

-- 与物体真实行为绑定
function M.bindObj(targetObj)
	M.bindedObj = targetObj

	local width = targetObj.contentBounds.xMax - targetObj.contentBounds.xMin
	local height = targetObj.contentBounds.yMax - targetObj.contentBounds.yMin
	local x =  targetObj.contentBounds.xMin + width*0.5
	local y =targetObj.contentBounds.yMin + height*0.5

	M.gRect.x, M.gRect.y = x, y
	M.gRect.rect.width, M.gRect.rect.height = width, height

end

local function createArrow(parent)
	local hRectHeight = 1
	local rectWidth = 130
	local hTranWidth = 6
	local tranHeight = 22
	local strokeWidth = 10

	local path1 = {
		0,0,
		0,-hRectHeight,
		rectWidth,-hRectHeight,
		rectWidth, -hTranWidth,
		rectWidth + tranHeight, 0,
		rectWidth, hTranWidth,
		rectWidth, hRectHeight,
		0, hRectHeight,
	}

	local arrow = display.newPolygon(-strokeWidth*0.5, 0, path1)
	arrow.anchorX = 0
	parent:insert(arrow)

	arrow.strokeWidth = strokeWidth
	arrow:setStrokeColor(1, 0, 1, 0.01)

	local path2 = {
		0, 0,
		-hRectHeight, 0,
		-hRectHeight, -rectWidth,
		-hTranWidth,  -rectWidth,
		0, -(rectWidth + tranHeight),
		hTranWidth, -rectWidth,
		hRectHeight, -rectWidth,
		hRectHeight, 0
	}
	local arrow1 = display.newPolygon(0, strokeWidth*0.5, path2)
	arrow1.anchorY = 1
	parent:insert(arrow1)

	arrow1.strokeWidth = strokeWidth
	arrow1:setStrokeColor(1,0, 1, 0.01)
end


local function onMove(event)
	if M.bindedObj then
		M.bindedObj.x = M.bindedObj.x + event.frameOffsetX
		M.bindedObj.y = M.bindedObj.y + event.frameOffsetY
		M.gRect.x = M.gRect.x + event.frameOffsetX
		M.gRect.y = M.gRect.y + event.frameOffsetY
		eventDispatcher:dispatchEvent("chooseObjMove", {target = M.bindedObj})
	end
end
-- 创建拖动矩形
function M.createDrag(params)
	if M.gRect then
		if M.gRect.proxyObj ~= params.target then
			M.bindObj(params.target)
		end
		return
	end

	local gRect = display.newGroup()
	--gRect.anchorChildren = true
	M.gRect = gRect
	gDrag:insert(M.gRect)

	local rect = display.newRect(0, 0, 30, 30)
	rect.strokeWidth = 2
	rect:setFillColor(1, 1, 1, 0.02)
	rect:setStrokeColor(unpack(colorCfg[10]))
	gRect.rect = rect
	gRect:insert(rect)
	M.bindObj(params.target)

	createArrow(gRect)

	inputUtils.touchDelegate({target = M.gRect, onMove = onMove})
end

function M.init()
	eventDispatcher:addEventListener("chooseObj", M.createDrag)
	eventDispatcher:addEventListener("clickHieObj", M.createDrag)
end

return M